﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using CarErp.Model;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace CarErp.Data
{
	[DbConfigurationType(typeof(MySqlEFConfiguration))]
	public class CarERPContext : DbContext
	{
		public CarERPContext()
			: base(ConnectionString)
		{ }

		public DbSet<User> Users { get; set; }
		public DbSet<CarPurchaseApplication> CarPurchaseApplications { get; set; }

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
			modelBuilder.Configurations.AddFromAssembly(this.GetType().Assembly);
		}

		private static string ConnectionString => _ConnectionString.Value;
		private static readonly Lazy<string> _ConnectionString = new Lazy<string>(GetConnectionString);
		private static string GetConnectionString()
		{
			var builder = new MySqlConnectionStringBuilder();
			builder.Server = Config.Server;
			builder.Port = Config.Port;
			builder.Database = Config.Database;
			builder.UserID = Config.UserName;
			builder.Password = Config.Password;
			return builder.ConnectionString;
		}
	}
}
